<?php 

class GeonameInputHelper extends Helper{
	
	var $helpers = array('Html');
	
	var $country_id = 'country_id';
	var $country_id_hidden = 'country_id_hidden';
	var $state_id = 'state_id';
	var $state_id_hidden = 'state_id_hidden';
	var $city_id = 'city_id';
	var $city_id_hidden = 'city_id_hidden';
	
	public function config($array){
		foreach ($array as $key=>$item){
			$this->$key = $item;
		}
	}
	
	public function begin(){
		$url = $this->Html->url('/', true);
		echo 
		
		"<script type=text/javascript>
				
		jQuery(function(){
		
		$(':input#$this->state_id').attr('disabled', true);
		$(':input#$this->state_id_hidden').attr('disabled', true);
		$(':input#$this->city_id').attr('disabled', true);
		$(':input#$this->city_id_hidden').attr('disabled', true);	

		$(':input#$this->country_id').typeahead({
			source: function(query, process) {
				objects = [];
				map = {};
				return $.get(
					'".$url."geonames/geonames/paises',
					{ query: query },
					function (data) {
						dados = JSON.parse(data);
						$.each(dados, function(i, object) {
							map[object.countryName] = object;
							objects.push(object.countryName);
						});
						$(':input#$this->city_id').attr('disabled', true);
						$(':input#$this->city_id_hidden').attr('disabled', true);	
						return process(objects);
					}
				);
		    },
		    updater: function(item) {
		        $(':input#$this->country_id_hidden').val(map[item].geonameId);
		        $(':input#$this->state_id').val('');
				$(':input#$this->state_id_hidden').val('');
				$(':input#$this->state_id').attr('disabled', false);
				$(':input#$this->state_id_hidden').attr('disabled', false);
				$(':input#$this->city_id').val('');
				$(':input#$this->city_id_hidden').val('');
		        return item;
		    }
		});

		$(':input#$this->state_id').typeahead({
			source: function(query, process) {
				objects = [];
				map = {};
				return $.get(
					'".$url."geonames/geonames/estados',
					{ query: query, parentId: $(':input#$this->country_id_hidden').val() },
					function (data) {
						dados = JSON.parse(data);
						$.each(dados, function(i, object) {
							map[object.name] = object;
							objects.push(object.name);
						});
						return process(objects);
					}
				);
		    },
		    updater: function(item) {
		        $(':input#$this->state_id_hidden').val(map[item].geonameId);
		        $(':input#$this->city_id').val('');
				$(':input#$this->city_id_hidden').val('');
				$(':input#$this->city_id').attr('disabled', false);
				$(':input#$this->city_id_hidden').attr('disabled', false);
		        return item;
		    }
		}); 	
		$(':input#$this->city_id').typeahead({
			source: function(query, process) {
				objects = [];
				map = {};
				return $.get(
					'".$url."geonames/geonames/cidades',
					{ query: query, parentId: $(':input#$this->state_id_hidden').val() },
					function (data) {
						dados = JSON.parse(data);
						$.each(dados, function(i, object) {
							map[object.name] = object;
							objects.push(object.name);
						});
						return process(objects);
					}
				);
		    },
		    updater: function(item) {
		        $(':input#$this->city_id_hidden').val(map[item].geonameId);
		        return item;
		    }
		}); 
		});
		</script>		
		";
	}
	
	public function findCountryName($geonameId){
		$this->Pais = ClassRegistry::init('Geonames.Pais');
		$pais = $this->Pais->find('first', array('conditions'=>array('geonameId'=>$geonameId, 'language'=>getLanguage())));
		return ($pais['Pais']['countryName']);
	}
	
	public function findStateName($geonameId){
		$this->Regiao = ClassRegistry::init('Geonames.RegiaoAdministrativa');
		$pais = $this->Regiao->find('first', array('conditions'=>array('geonameId'=>$geonameId, 'language'=>getLanguage())));
		return ($pais['RegiaoAdministrativa']['name']);
	}
	
	public function findCityName($geonameId){
		$this->Cidade = ClassRegistry::init('Geonames.Cidade');
		$pais = $this->Cidade->find('first', array('conditions'=>array('geonameId'=>$geonameId, 'language'=>getLanguage())));
		return ($pais['Cidade']['name']);
	}
	
}

?>